package org.pp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.pp.core.Assertion;
import org.pp.core.BizExcept;
import org.pp.core.MiniappSessionUtil;
import org.pp.core.PageBean;
import org.pp.dto.PageDto;
import org.pp.entity.PpCart;
import org.pp.entity.PpGoodsSku;
import org.pp.mapper.PpCartMapper;
import org.pp.miniapp.dto.CartAdd;
import org.pp.miniapp.dto.CartSet;
import org.pp.service.IPpCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.pp.service.IPpGoodsSkuService;
import org.pp.store.OssStore;
import org.pp.util.PageUtil;
import org.pp.vo.MiniappCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author haima
 * @since 2024-10-02
 */
@Service
public class PpCartServiceImpl extends ServiceImpl<PpCartMapper, PpCart> implements IPpCartService {

    @Autowired
    IPpGoodsSkuService skuService;

    @Autowired
    OssStore store;

    @Override
    public Long add(CartAdd dto) {
        PpGoodsSku sku = skuService.getById(dto.getSkuId());
        Assertion.nullThrow(sku,"商品不存在");
        if(dto.getAmount()>sku.getStock()){
            throw BizExcept.erro("库存不足");
        }
        Long userId=MiniappSessionUtil.getUserId();
        PpCart one = getOne(new LambdaQueryWrapper<PpCart>()
                .eq(PpCart::getType, dto.getType())
                .eq(PpCart::getUserId, userId)
                .eq(PpCart::getSkuId, dto.getSkuId()));

        if(Objects.isNull(one)){
            if(dto.getAmount()<=0){
                throw BizExcept.erro("商品数需大于0");
            }
            PpCart cart = BeanUtil.copyProperties(dto, PpCart.class);
            cart.setUserId(userId);
            save(cart);
            return cart.getCartId();
        }else{
            PpCart edit= new PpCart();
            edit.setCartId(one.getCartId());
            edit.setAmount(one.getAmount()+ dto.getAmount());
            if(Objects.equals(dto.getType(),true)){
                //直接购买，直接设置数量
                edit.setAmount(dto.getAmount());
            }
            if(edit.getAmount()>sku.getStock()){
                throw BizExcept.erro("库存不足");
            }
            if(edit.getAmount()<=0){
                throw BizExcept.erro("商品数需大于0");
            }
            updateById(edit);
            return one.getCartId();
        }
    }

    @Override
    public void skuSet(CartSet dto) {
        PpCart cart = getById(dto.getCartId());
        Assertion.nullThrow(cart,"商品条目不存在");
        PpGoodsSku sku = skuService.getById(cart.getSkuId());
        Assertion.nullThrow(sku,"商品条目不存在");
        if(dto.getAmount()>sku.getStock()){
            throw BizExcept.erro("库存不足");
        }
        if(dto.getAmount()<=0){
            throw BizExcept.erro("商品数需大于0");
        }

        PpCart edit= new PpCart();
        edit.setCartId(dto.getCartId());
        edit.setAmount(dto.getAmount());
        updateById(edit);
    }

    @Override
    public void delete(Long cartId) {
        PpCart cart = getById(cartId);
        Assertion.nullThrow(cart,"商品条目不存在");
        removeById(cartId);
    }

    @Override
    public Long getCount() {
        Long userId = MiniappSessionUtil.getUserId(false);
        if(Objects.isNull(userId)){
            return 0L;
        }
        return baseMapper.getCount(userId);
    }

    @Override
    public PageBean<MiniappCartVo> pageList(PageDto dto) {
        if(dto.isPageShow()){
            PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        }
        Long userId = MiniappSessionUtil.getUserId();
        List<MiniappCartVo> list= baseMapper.pageList(userId);
        for (MiniappCartVo vo : list) {
            vo.setCover(store.getUrl(vo.getCover()));
            vo.setAttrInfoStr(CollUtil.join(vo.getAttrInfo().values(),","));
        }
        return PageUtil.load(new PageInfo<>(list),MiniappCartVo.class);
    }


}
